package middleware

import (
	"el/app/models"
	"el/app/models/request"

	"github.com/labstack/echo/v4"
)

func AdminLog(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		adminLog := models.NewAdminLog()
		adminLog.Method = c.Request().Method
		adminLog.Path = c.Path()
		adminLog.Ip = c.RealIP()

		claimsInterface := c.Get("claims")
		if claimsInterface != nil {
			claims := claimsInterface.(*request.Claims)
			adminLog.AdminId = int(claims.Id)
		}
		err := next(c)
		adminLog.HttpCode = c.Response().Status
		adminLog.Create()
		return err
	}
}
